package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Destination;
import cn.wolfcode.domain.Region;
import cn.wolfcode.query.DestinationQuery;
import cn.wolfcode.repository.DestinationRepository;
import cn.wolfcode.repository.RegionRepository;
import cn.wolfcode.service.IDestinationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationRepository repository;
    @Autowired
    private RegionServiceImpl regionService;
    @Autowired
    private DestinationServiceImpl destinationService;
    @Autowired
    private MongoTemplate template;

    @Override
    public void insert(Destination user) {
        repository.save(user);
    }

    @Override
    public void update(Destination user) {
        repository.save(user);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public Destination get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<Destination> listAll() {
        return repository.findAll();
    }

    @Override
    public List<Destination> getDestByRegionId(String rid) {
        Region region = regionService.get(rid);
        List<String> ids = region.getRefIds();
        List<Destination> list = repository.findByIdIn(ids);
        return list;
    }

    @Override
    public Page query(DestinationQuery qo) {
        //创建条件对象
        Query query = new Query();
        //查询总条数
        long total = template.count(query, Destination.class);
        if (total == 0){
            return Page.empty();
        }
        //关键字高查
        if (StringUtils.hasLength(qo.getKeyword())){
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }

        //吐司
        if (StringUtils.hasLength(qo.getParentId())){
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }else {
            query.addCriteria(Criteria.where("parentId").is(null));
        }

        PageRequest page = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        query.with(page);

        List<Destination> list = template.find(query, Destination.class);
        PageImpl<Destination> destinations = new PageImpl<>(list, page, total);
        return destinations;
    }

    @Override
    public List<Destination> queryToasts(String parentId) {
        List<Destination> list = new ArrayList<>();
        createToasts(list,parentId);
        Collections.reverse(list);
        return list;
    }


    private void createToasts(List<Destination> list,String parentId){
        if (!StringUtils.hasLength(parentId)){
            return;
        }
        Destination destination = destinationService.get(parentId);
        list.add(destination);
        //如果有父类Id,就一直调用自己
        if (StringUtils.hasLength(destination.getParentId())){
            createToasts(list,destination.getParentId());
        }
    }

    @Override
    public List<Destination> getDestAllById(String rid) {
        List<Destination> list = new ArrayList<>();
        if ("-1".equals(rid)){
            list = repository.findByParentName("中国");
            for (Destination dest : list){
                Pageable pageable = PageRequest.of(0, 5, Sort.Direction.ASC, "id");
                List<Destination> destinations = repository.findByParentId(dest.getId(),pageable);
                dest.setChildren(destinations);
            }
        }else {
            list = this.getDestByRegionId(rid);
            for (Destination destination : list){
                Pageable pageable = PageRequest.of(0,5, Sort.Direction.ASC,"id");
                List<Destination> destinations = repository.findByParentId(destination.getId(), pageable);
                destination.setChildren(destinations);
            }
        }
        return list;
    }


}
